﻿
@{
    Layout = null;
}

<link href="~/lib/layui/css/layui.css" rel="stylesheet" />
<style>
    .layui-form-label {
        width: 160px
    }

    .layui-input-block {
        margin-left: 200px;
        width: 186px;
    }

    .layui-form-item {
        margin-bottom: 30px; /* 每个行之间的距离 */
        width: 400px
    }

</style>
<form class="layui-form" action="">
    <input type="hidden" name="Id" id="Id" class="layui-input">
    <table>
        <tr>
            <td>
                <div class="layui-form-item">
                    <label class="layui-form-label">客户编码</label>
                    <div class="layui-input-block">
                        <input type="text" name="CustomerCode" id="CustomerCode" lay-verify="required" placeholder="请输入客户编码" autocomplete="off" class="layui-input">
                        <input type="checkbox" name="autoGenerate" lay-skin="switch" lay-filter="switchTest">自动生成
                    </div>

                </div>
            </td>
            <td>
                <div class="layui-form-item">
                    <label class="layui-form-label">客户名称</label>
                    <div class="layui-input-block">
                        <input type="text" id="CustomerName" name="CustomerName" lay-verify="required" placeholder="请输入客户名称" autocomplete="off" class="layui-input">
                    </div>
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <div class="layui-form-item">
                    <label class="layui-form-label">客户简称</label>
                    <div class="layui-input-block">
                        <input type="text" id="CustomerAbbreviation" name="CustomerAbbreviation" lay-verify="required" placeholder="请输入客户简称" autocomplete="off" class="layui-input">
                    </div>
                </div>
            </td>
            <td>
                <div class="layui-form-item">
                    <label class="layui-form-label">客户类型</label>
                    <div class="layui-input-inline">
                        @* 下拉框默认值选择：selected *@
                        <select id="CustomerType" name="CustomerType" lay-filter="aihao">
                            <option value=""></option>
                            <option value="1">企业</option>
                            <option value="2">个人</option>
                        </select>
                    </div>
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <div class="layui-form-item">
                    <label class="layui-form-label">客户电话</label>
                    <div class="layui-input-block">
                        <input type="text" id="CustomerPhone" name="CustomerPhone" lay-verify="required" placeholder="请输入客户电话" autocomplete="off" class="layui-input">
                    </div>
                </div>
            </td>
            <td>
                <div class="layui-form-item">
                    <label class="layui-form-label">联系人</label>
                    <div class="layui-input-block">
                        <input type="text" id="ContactName" name="ContactName" lay-verify="required" placeholder="请输入联系人" autocomplete="off" class="layui-input">
                    </div>
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <div class="layui-form-item">
                    <label class="layui-form-label">联系人电话</label>
                    <div class="layui-input-block">
                        <input type="text" id="ContactPhone" name="ContactPhone" lay-verify="required" placeholder="请输入联系人电话" autocomplete="off" class="layui-input">
                    </div>
                </div>
            </td>
            <td>
                <div class="layui-form-item">
                    <label class="layui-form-label">是否启用</label>
                    <div class="layui-input-block">
                        <input type="radio" name="State" value="true" title="启用" checked>
                        <input type="radio" name="State" value="false" title="禁用">
                    </div>
                </div>
            </td>
        </tr>
    </table>
    <div class="layui-form-item">
        <div class="layui-input-block" style="margin-left:400px;margin-top:30px">
            <button type="submit" class="layui-btn" lay-submit lay-filter="edit">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<script src="~/lib/jquery/dist/jquery.js"></script>
<script src="~/lib/layui/layui.js"></script>

<script>
    $(function () {
        getManageById()
    })
    
    function getManageById() {
        $.ajax({
            url: "/CustomerManagement/GetCustomerById",
            data: { id: @ViewBag.id},
            type: "get",
            datatype: "json",
            success: function (res) {

                $("#Id").val(res.data.id);
                $("#CustomerCode").val(res.data.customerCode);
                $("#CustomerName").val(res.data.customerName);
                $("#CustomerAbbreviation").val(res.data.customerAbbreviation);
                $("#CustomerType").val(res.data.customerType);
                $("#CustomerPhone").val(res.data.customerPhone);
                $("#ContactName").val(res.data.contactName);
                $("#ContactPhone").val(res.data.contactPhone);
                $("[name='State'][value="+res.data.state+"]").prop("checked",true);

                layui.form.render("radio")
                layui.form.render("select")
            }
        })
    }

    layui.use(['form', 'upload', 'layer', 'element'], function () {
        var form = layui.form;
        var layer = layui.layer;
        var upload = layui.upload;
        var element = layui.element;

        // 监听复选框变化，自动生成客户编码
        form.on('switch(switchTest)', function (data) {
            if (data.elem.checked) {
                // 生成随机字母+数字的编码并设置到CustomerCode输入框中
                var randomNum = Math.floor(Math.random() * 10) + '';
                for (var i = 0; i < 4; i++) {
                    randomNum += Math.floor(Math.random() * 10);
                }
                randomNum = 'C00' + randomNum;
                $('input[name="CustomerCode"]').val(randomNum);
            }
            else
            {
                $('input[name="CustomerCode"]').val('');
            }
        });


        // 提交事件
        form.on('submit(edit)', function (data) {
            var field = data.field; // 获取表单字段值
            $.ajax({
                url: "/CustomerManagement/PutCustomerManagementInfo",
                type: "post",
                async: false,
                data: field,
                datatype: "json",
                success: function (res) {
                    console.log(22,res)
                    if (res.code > 0) {
                        layer.alert('修改成功', { title: '操作结果' }, function () {
                            var index = parent.layer.getFrameIndex(window.name); // 获取当前 iframe 层的索引
                            parent.layer.close(index); // 关闭当前 iframe 层
                            parent.location.reload(); // 刷新父页面
                        });
                    }
                }
            });
            return false; // 阻止默认 form 跳转
        });

    })
</script>